package m2;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection conn = f.newConnection();
        Channel channel = conn.createChannel();// 使用这个通信通道进行通信

        // 定义队列
        channel.queueDeclare("task_queue", true, false, false, null);

        // 发送消息
        // 可以发送模拟延迟消息 : "edf.ef...fas....asdf.fafs..f"
        while (true) {
            System.out.println("请输入消息: ");
            String msg = new Scanner(System.in).nextLine();
            channel.basicPublish("", "task_queue", MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
        }

    }

}
